#include <stdio.h>
#include "../global.h"
#include <string.h>
#include "queue.h"


int queueArr[QUEUESIZE];
/*
* 队列空间可以是 静态的栈中，也可以动态的申请
* 队列的初始化
* 队列 增 ,传入该数据的指针
* 队列 减
* 获取队列的大小
* 获取当前队列的长度
* 判断队列是否为空
* 打印出队列中的所有数据
* 队列的销毁
*/

void InitQueue(QUEUE* que,void* quePointer)
{
	que->quePointer = quePointer;
	que->top = 0;
	que->tail = -1;
	que->size = QUEUESIZE;
}

/*
* 0-fali 1-success
*/
int EnterQueue(QUEUE* que,const int* const data)
{
	if(que->quePointer == NULL) return 0;
	if(que->tail >= QUEUESIZE) 
	{
		printf("queue overflow!!!");
		return 0;
	}
	que->tail++;
	que->quePointer[que->tail] = *data;
	return 1;
}

/*
* 
*/
int OutQueue(QUEUE* que)
{

	if(que->quePointer == NULL) return INTERROR;
	if(que->tail < que->top) printf("queue is empety!!!");
	int tmp;
	tmp = que->quePointer[que->top];
	que->top++;
	return tmp;
}

/*
* 获取队列当前的长度
*/
int LenQueue(QUEUE* que)
{
	return (que->tail - que->top+1);
}

/*
* 打印出队列的所有元素，从队头开始打印
*/
void PrintQueue(QUEUE* que)
{
	int i =0;
	int len = LenQueue(que);
	for(i=0;i<len;i++)
	{
		printf("%d ",que->quePointer[que->top+i]);

	}
}

/*
* 释放当前队列的空间
*/
void FreeQueue(QUEUE* que)
{
	FREE(que->quePointer);
}

#ifdef QUEUE_DEBUG
int main(int argc, char const *argv[])
{
	int i=0;
	QUEUE* que;

	int* quePointer = (int*)malloc(sizeof(int)*QUEUESIZE);
	InitQueue(que,quePointer);
	for(i=0;i<QUEUESIZE;i++)
	{
		EnterQueue(que,&i);
	}
	int len = LenQueue(que);
	printf("%d\n",LenQueue(que));
	
	PrintQueue(que);
	

	printf("\n-----------------------\n");
	FreeQueue(que);
	return 0;
}
#endif
